import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import LoginComponent from '@/components/LoginComponent.vue'
const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect: '/login', // 首次访问重定向到登录页面
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/components/LoginComponent.vue'),
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('@/components/Register.vue'), // 注册页面的组件
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('@/view/Home/home.vue'),
  },
  {
    path: '/make',
    name: 'make',
    component: () => import('@/view/Make/Make.vue'),
  },
  {
    path: '/create',
    name: 'create',
    component: () => import('@/view/CreateTemplate/CreateTemplate.vue'),
  },
  {
    path: '/customize',
    name: 'customize',
    component: () => import('@/view/CreateTemplate/customizeResume.vue'),
  },
  {
    //在线工具
    path: '/compression',
    name: 'compression',
    component: () => import('@/components/Compression.vue'),
  },
  {
    path: '/guide',
    name: 'guide',
    component: () => import('@/components/Guide.vue'),
  },
]
const router = createRouter({
  history: createWebHashHistory(),
  routes: routes,
})
// 添加 beforeEach 导航守卫
// router.beforeEach((to, from, next) => {
//   // 在每次路由跳转之前，将滚动条位置设置为页面顶部
//   window.scrollTo(0, 0)
//   // 继续路由跳转
//   next()
// })

// 模拟用户登录状态
const isAuthenticated = () => {
  return localStorage.getItem('loggedIn') === 'true'
}
// 导航守卫，用于控制路由访问权限
router.beforeEach((to, from, next) => {
  if (to.path !== '/login' && to.path !== '/register' && !isAuthenticated()) {
    // 如果不是访问登录页和注册页且未登录，则重定向到登录页面
    next('/login')
  } else {
    // 其他情况允许访问
    next()
  }
})
export default router
